package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.adapter.MyListAdapter;
import com.example.myapplication.util.OkHttpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private ListView vl;



    @SuppressLint("CommitPrefEdits")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE);
        SharedPreferences.Editor edit = data.edit();

        Button bu1 = findViewById(R.id.bu1);
        TextView te1 = findViewById(R.id.te1);
        vl = findViewById(R.id.lv);
//        vl.setAdapter(new MyListAdapter(MainActivity.this));






        bu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                te1.setText(data.getString("name","获取失败"));

//                userList();
//                post();
            }
        });


    }
    public void userList(){
        ObjectMapper objectMapper = new ObjectMapper();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpUtil.okGet("/userAll", new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            Log.d("请求", "请求失败");
                        }
                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            String users = response.body().string();
                            
                            ArrayList<Map> user = objectMapper.readValue(users, new TypeReference<ArrayList<Map>>() {});
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    vl.setAdapter(new MyListAdapter(MainActivity.this,user));

                                }
                            });
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                    Log.e("原因", "run: 11111111111111111111111111111", e);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT);
                        }
                    });
                }
            }
        }).start();

    }
    public void post(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id",5555);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                OkHttpUtil.okPost("/getId", jsonObject.toString(), new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.d("请求", "onFailure: 请求失败");
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        Log.d("请求", string);
                        FormBody build = new FormBody.Builder().build();
                    }
                });
            }
        }).start();
    }
}